package com.liuzy.openapi;

import java.util.HashMap;
import java.util.Map;

import com.liuzy.tool.HTTP;
import com.liuzy.tool.JSON;
import com.liuzy.util.Util;

/**
 * 综合快递查询-快递100+爱查快递+小米58
 * @author liuzy
 * @since 2015年10月18日
 */
public class ExpressApi {
	public static void main(String[] args) {
		String exNum = "368110985904";
		System.out.println(query(exNum));
//		System.out.println(kd100(exNum));
//		System.out.println(ickd(exNum));
//		System.out.println(xm58(exNum));
	}
	private static boolean KD100 = true;
	private static boolean ICKD = true;
	private static boolean XM58 = true;
	private static String data = "[]";
	private static String exName = "";
	private static String exTel = "";
	public static String query(String exNum) {
		if (KD100 && kd100(exNum) != null) {}
		else if (ICKD && ickd(exNum) != null) {}
		else if (XM58 && xm58(exNum) != null) {}
		ExCom exCom = exComs.get(exName);
		if (exCom != null) {
			exName = exCom.getName();
			exTel = exCom.getTel();
		}
		return "{\"exNum\":\""+exNum+"\",\"exName\":\""+exName+"\",\"exTel\":\""+exTel+"\",\"data\":"+data+"}";
	}
	public static String kd100(String exNum) {
		String exCom = kd100ExCom(exNum);
		if (exCom != null) {
			HTTP client = new HTTP();
			String url = "http://www.kuaidi100.com/query";
			client.put2url("type", exCom).put2url("postid", exNum);
			String jsonString = client.doGET(url);
			Util.log("KD100", jsonString);
			try {
				if (jsonString.contains("data")) {
					JSON json = new JSON(jsonString);
					data = json.getArray("data").value();
					exName = json.get("com").value();
					exTel = "";
					return data;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	private static String kd100ExCom(String exNum) {
		HTTP http = new HTTP();
		String url = "http://www.kuaidi100.com/autonumber/autoComNum";
		http.put2url("text", exNum);
		String jsonString = http.doGET(url);
		Util.log("KD100", jsonString);
		try {
			JSON json = new JSON(jsonString);
			return json.getArray("auto", 0).get("comCode").value();
		} catch (Exception e) {
			return null;
		}
	}
	public static String ickd(String exNum) {
		String exCom = ickdExCom(exNum);
		if (exCom != null) {
			HTTP http = new HTTP();
			String url = "http://api.ickd.cn/";
			http.put2url("id", exNum)
			.put2url("nu", exNum)
			.put2url("com", exCom);
			http.setReadGBK();
			String jsonString = http.doGET(url);
			Util.log("ICKD", jsonString);
			try {
				if (jsonString.contains("data")) {
					JSON json = new JSON(jsonString);
					data = json.getArray("data").value();
					exName = json.get("expTextName").value();
					exTel = json.get("tel").value();
					return data;
				}
			} catch (Exception e) {
			}
		}
		return null;
	}
	private static String ickdExCom(String exNum) {
		HTTP http = new HTTP();
		String url = "http://api.ickd.cn/autoCom/" + exNum;
		String jsonString = http.doGET(url);
		Util.log("ICKD", jsonString);
		try {
			JSON json = new JSON(jsonString);
			return json.getArray("coms", 0).get("com").value();
		} catch (Exception e) {
			return null;
		}
	}
	public static String xm58(String exNum) {
		HTTP http = new HTTP();
		String url = "http://xiaomi58.com/kd/do.php";
		http.put2body("mailNo", exNum).put2body("com", "");
		http.setReadGBK();
		String jsonString = http.doPOST(url);
		Util.log("XM58", jsonString);
		try {
			if (jsonString.contains("data")) {
				JSON json = new JSON(jsonString);
				data = json.getArray("data").value();
				exName = json.get("expTextName").value();
				exTel = json.get("tel").value();
				return data;
			}
		} catch (Exception e) {
		}
		return null;
	}

	public static Map<String, ExCom> exComs = new HashMap<>();
	static {
		exComs.put("aae", new ExCom("AAE快递", "400-610-0400"));
		exComs.put("aj", new ExCom("安捷快递", "400-056-5656"));
		exComs.put("anjie", new ExCom("安捷快递", "0532-81636315"));
		exComs.put("anneng", new ExCom("安能物流", "400-104-0088"));
		exComs.put("anwl", new ExCom("安能物流", "40010-40088"));
		exComs.put("anxinda", new ExCom("安信达", "021-54224681"));
		exComs.put("anxun", new ExCom("安迅物流", "010-59288730"));
		exComs.put("axd", new ExCom("安信达快递", "021-54224681"));
		exComs.put("bafudongfang", new ExCom("百福东方快递", "010-57169000"));
		exComs.put("balunzhi", new ExCom("巴伦支", "400-636-1516"));
		exComs.put("bangsong", new ExCom("邦送物流", "021-20965696"));
		exComs.put("bfdf", new ExCom("百福东方", "010-57169000"));
		exComs.put("bgn", new ExCom("布谷鸟快递", "400-100-0533"));
		exComs.put("bgpyghx", new ExCom("包裹、平邮、挂号信", "11185"));
		exComs.put("bht", new ExCom("BHT快递", "010-58633508"));
		exComs.put("cces", new ExCom("CCES快递", "4006773777"));
		exComs.put("chengguang", new ExCom("程光快递", "006499482780"));
		exComs.put("chengji", new ExCom("城际快递", "4000-523-525"));
		exComs.put("chuangxi", new ExCom("传喜物流", "400-777-5656 "));
		exComs.put("chuanzhi", new ExCom("传志快递", "021-5632555556625555 010-67035843 020-86380996"));
		exComs.put("citylink", new ExCom("City-Link", "603-55658399"));
		exComs.put("coe", new ExCom("东方快递", "0755-83575000"));
		exComs.put("cs", new ExCom("城市100快递", "010-52932760"));
		exComs.put("cszx", new ExCom("城市之星物流", "400-628-5168"));
		exComs.put("ctwl", new ExCom("长通物流", "0731-66520111"));
		exComs.put("cxwl", new ExCom("传喜物流", "400-777-5656"));
		exComs.put("datian", new ExCom("大田物流", "400-626-1166"));
		exComs.put("dayang", new ExCom("大洋物流", "400-820-0088"));
		exComs.put("ddwl", new ExCom("大达物流", "400-098-5656"));
		exComs.put("debang", new ExCom("德邦物流", "400-830-5555"));
		exComs.put("dhl", new ExCom("DHL快递", "800-810-8000"));
		exComs.put("dida", new ExCom("递达速运", "400-687-8123"));
		exComs.put("disifang", new ExCom("递四方", "0755-33933895"));
		exComs.put("dpex", new ExCom("DPEX快递", "021-64659883"));
		exComs.put("ds", new ExCom("D速快递", "0531-88636363"));
		exComs.put("dsf", new ExCom("递四方速递", "0755-33933895"));
		exComs.put("dsu", new ExCom("D速快递", "0531-88636363"));
		exComs.put("dtwl", new ExCom("大田物流", "010-59237777"));
		exComs.put("dywl", new ExCom("大洋物流", "400-820-0088"));
		exComs.put("ems", new ExCom("EMS快递", "11183"));
		exComs.put("fanke", new ExCom("凡客配送", "400-010-6660"));
		exComs.put("fanyu", new ExCom("凡宇快递", "400-658-0358"));
		exComs.put("fbwl", new ExCom("飞邦物流", "0769-89066698"));
		exComs.put("febao", new ExCom("飞豹快递", "400-000-5566"));
		exComs.put("fedex", new ExCom("FEDEX国际快递", "400-886-1888"));
		exComs.put("fedexcn", new ExCom("FEDEX国内快递", "400-886-1888"));
		exComs.put("feibao", new ExCom("飞豹快递", "400-000-5566"));
		exComs.put("feihang", new ExCom("飞航物流", "0755-29778899"));
		exComs.put("feihu", new ExCom("飞狐快递", "010-51389299"));
		exComs.put("feiyang", new ExCom("飞洋快递", "18773879799 626-527-5800"));
		exComs.put("fengda", new ExCom("丰达快递", "400-113-6666"));
		exComs.put("fengxingtianxia", new ExCom("风行天下快递", "400-040-4909"));
		exComs.put("fkd", new ExCom("飞康达快递", "010-84223376 010-84223378"));
		exComs.put("fkdex", new ExCom("飞快达快递", "400-716-6666"));
		exComs.put("gdems", new ExCom("广东ems快递", "11183"));
		exComs.put("gjbg", new ExCom("国际包裹", "11183"));
		exComs.put("gls", new ExCom("GLS快递", "877-914-5465"));
		exComs.put("gongsuda", new ExCom("共速达", "400-111-0005"));
		exComs.put("gsdwl", new ExCom("共速达物流", "400-111-0005"));
		exComs.put("gsm", new ExCom("GSM快递", "021-64656011 "));
		exComs.put("gtsd", new ExCom("高铁速递", "400-999-7777"));
		exComs.put("guada", new ExCom("冠达快递", "400-990-0088"));
		exComs.put("guangtong", new ExCom("广通速递", "400-080-6369"));
		exComs.put("guotong", new ExCom("国通快递", "4006-773-777"));
		exComs.put("gznd", new ExCom("港中能达", "400-620-1111"));
		exComs.put("haihong", new ExCom("山东海红快递", "400-632-9988"));
		exComs.put("haimeng", new ExCom("海盟速递", "400-080-6369 "));
		exComs.put("haiwaihuangqiu", new ExCom("海外环球", "010-59790107"));
		exComs.put("haosheng", new ExCom("昊盛物流", "400-186-5566"));
		exComs.put("henglu", new ExCom("恒路物流", "400-182-6666"));
		exComs.put("hswl", new ExCom("昊盛物流", "400-186-5566"));
		exComs.put("huaqi", new ExCom("华企快递", "400-806-8111"));
		exComs.put("huaxialong", new ExCom("华夏龙", "0755-61211999"));
		exComs.put("huayu", new ExCom("天地华宇快递", "400-808-6666"));
		exComs.put("huiqiang", new ExCom("汇强快递", "400-000-0177"));
		exComs.put("huitong", new ExCom("汇通快递", "021-62963636"));
		exComs.put("huitongkuaidi", new ExCom("汇通快递", "021-62963636"));
		exComs.put("hxlwl", new ExCom("华夏龙物流", "0755-61211999"));
		exComs.put("jiaji", new ExCom("佳吉快运", "400-820-5566"));
		exComs.put("jiali", new ExCom("嘉里大通", "400-610-3188"));
		exComs.put("jianhua", new ExCom("河北建华", "0311-86123186"));
		exComs.put("jiayi", new ExCom("佳怡物流", "400-631-9999"));
		exComs.put("jiayunmei", new ExCom("加运美快递", "0769-8551-5555"));
		exComs.put("jietie", new ExCom("捷特快递", "400-820-8585"));
		exComs.put("jingda", new ExCom("金大物流", "0755-82262209"));
		exComs.put("jingdong", new ExCom("京东快递", "400-603-3600"));
		exComs.put("jingguang", new ExCom("京广快递", "0769-83660666-60"));
		exComs.put("jingyue", new ExCom("晋越快递", "0769-85158039"));
		exComs.put("jinyue", new ExCom("晋越快递", "0769-85158039"));
		exComs.put("jixianda", new ExCom("急先达", "400-694-1256"));
		exComs.put("jldt", new ExCom("嘉里大通物流", "400-610-3188"));
		exComs.put("jywl", new ExCom("佳怡物流", "400-660-5656"));
		exComs.put("kangli", new ExCom("康力物流", "400-156-5156 "));
		exComs.put("kcs", new ExCom("KCS快递", "800-858-5590"));
		exComs.put("klwl", new ExCom("康力物流", "400-156-5156"));
		exComs.put("krwl", new ExCom("宽容物流", "400-820-2719"));
		exComs.put("kuaijie", new ExCom("快捷快递", "400-830-4888"));
		exComs.put("kuayue", new ExCom("跨越速递", "400-809-8098 "));
		exComs.put("lanhu", new ExCom("蓝狐快递", "4000661646"));
		exComs.put("lejie", new ExCom("乐捷递", "400-618-1400"));
		exComs.put("lejiedi", new ExCom("乐捷递快递", "400-618-1400"));
		exComs.put("lianbang", new ExCom("联邦快递", "400-889-1888"));
		exComs.put("lianhaotong", new ExCom("联昊通快递", "0769-88620000"));
		exComs.put("lijisong", new ExCom("立即送", "400-028-5666"));
		exComs.put("lingdao", new ExCom("林道国际快递", "400-820-0112"));
		exComs.put("longbang", new ExCom("龙邦快递", "021-39283333"));
		exComs.put("meiguo", new ExCom("美国快递", "888-611-1888"));
		exComs.put("meilong", new ExCom("美龙快递", "323-208-9848"));
		exComs.put("mengdumeng", new ExCom("门对门", "400-700-7676"));
		exComs.put("minbang", new ExCom("民邦快递", "0769-81515303"));
		exComs.put("minhang", new ExCom("民航快递", "400-817-4008"));
		exComs.put("minliang", new ExCom("明亮物流", "400-035-6568"));
		exComs.put("minsheng", new ExCom("闽盛快递", "0592-3725988"));
		exComs.put("nengda", new ExCom("能达速递", "400-6886-765"));
		exComs.put("newl", new ExCom("尼尔物流", "400-000-5611"));
		exComs.put("nuoyaao", new ExCom("偌亚奥快递", "400-887-1871"));
		exComs.put("ocs", new ExCom("OCS快递", "400-118-8588"));
		exComs.put("ontrac", new ExCom("OnTrac快递", "800-334-5000"));
		exComs.put("pinganda", new ExCom("平安达快递", "4006-230-009"));
		exComs.put("pingsuxingda", new ExCom("品速心达快递", "400-800-3693 "));
		exComs.put("pingyou", new ExCom("中国邮政平邮", "11183"));
		exComs.put("qitianliansuo", new ExCom("七天连锁快递", "400-882-1202"));
		exComs.put("quanchen", new ExCom("全晨快递", "0769-82026703"));
		exComs.put("quancheng", new ExCom("全晨快递", "0769-82026703"));
		exComs.put("quanfeng", new ExCom("全峰快递", "400-100-0001"));
		exComs.put("quanjitong", new ExCom("全际通", "400-0179-888"));
		exComs.put("quanritong", new ExCom("全日通快递", "020-86298999"));
		exComs.put("quanyi", new ExCom("全一快递", "400-663-1111"));
		exComs.put("rrs", new ExCom("日日顺物流", "400-999-999"));
		exComs.put("rufeng", new ExCom("如风达", "400-010-6660"));
		exComs.put("rufengda", new ExCom("如风达快递", "400-010-6660"));
		exComs.put("saiaodi", new ExCom("赛澳递", "4000-345-888"));
		exComs.put("santai", new ExCom("三态速递", "400-881-8106"));
		exComs.put("shangda", new ExCom("上大物流", "021-54477891"));
		exComs.put("shengan", new ExCom("圣安物流", "4006-618-169"));
		exComs.put("shengfeng", new ExCom("盛丰物流", "0591-83621111"));
		exComs.put("shenghui", new ExCom("盛辉物流", "4008-222-222"));
		exComs.put("shentong", new ExCom("申通快递", "400-889-5543"));
		exComs.put("shiyun", new ExCom("世运快递", "400-666-1111"));
		exComs.put("shunfeng", new ExCom("顺丰快递", "400-811-1111"));
		exComs.put("stwl", new ExCom("速通物流", "4006561185"));
		exComs.put("suer", new ExCom("速尔快递", "400-882-2168"));
		exComs.put("tcwl", new ExCom("通成物流", "021-36161199-8021"));
		exComs.put("tdhy", new ExCom("天地华宇", "400-808-6666"));
		exComs.put("thtx", new ExCom("通和天下物流", "400-0056-516"));
		exComs.put("tiantian", new ExCom("天天快递", "400-820-8198"));
		exComs.put("tnt", new ExCom("TNT快递", "800-820-9868"));
		exComs.put("tonghe", new ExCom("通和天下快递", "400-0056-516 "));
		exComs.put("ups", new ExCom("UPS国际快递", "400-820-8388"));
		exComs.put("usps", new ExCom("USPS快递", "800-275-8777"));
		exComs.put("wanbo", new ExCom("万博快递", "0311-83035553"));
		exComs.put("wanjia", new ExCom("万家物流", "021-5193 7018"));
		exComs.put("wanxiang", new ExCom("万象物流", "400-820-8088"));
		exComs.put("weibang", new ExCom("伟邦快递", "021-64212098 021-62629520"));
		exComs.put("weitepai", new ExCom("微特派快递", "400-606-0909"));
		exComs.put("wxwl", new ExCom("万象物流", "400-820-8088"));
		exComs.put("xfwl", new ExCom("信丰物流", "400-830-6333"));
		exComs.put("xianglong", new ExCom("祥龙运通", "0755-88888908"));
		exComs.put("xinbang", new ExCom("新邦物流", "4008-000-222"));
		exComs.put("xindan", new ExCom("新蛋物流", "400-820-4400"));
		exComs.put("xinfeihong", new ExCom("鑫飞鸿速递", "021-39777777"));
		exComs.put("xinfeng", new ExCom("信丰物流", "400-830-6333"));
		exComs.put("xingchen", new ExCom("星晨急便", "400-668-8400"));
		exComs.put("xiyoute", new ExCom("希优特", "400-840-0365"));
		exComs.put("xlyt", new ExCom("祥龙运通", "4008-908-908"));
		exComs.put("yad", new ExCom("源安达快递", "0769-85021875"));
		exComs.put("yafeng", new ExCom("亚风快递", "400-628-0018"));
		exComs.put("ycwl", new ExCom("远成物流", "400-820-1646"));
		exComs.put("yibang", new ExCom("一邦快递", "4008-000-666"));
		exComs.put("yiling", new ExCom("亿领速运", "400-611-1892"));
		exComs.put("yingjie", new ExCom("银捷速递", "0755-88250666"));
		exComs.put("yishunhang", new ExCom("亿顺航", "4006-018-268 "));
		exComs.put("yitongda", new ExCom("易通达", "0898-65339299"));
		exComs.put("youbijia", new ExCom("邮必佳", "400-687-8123"));
		exComs.put("yousu", new ExCom("优速快递", "400-1111-119"));
		exComs.put("yuananda", new ExCom("源安达", "0769-85021875"));
		exComs.put("yuancheng", new ExCom("远成物流", "400-820-1646"));
		exComs.put("yuanfeihang", new ExCom("原飞航快递", "0755-29778899"));
		exComs.put("yuantong", new ExCom("圆通快递", "021-69777888-999"));
		exComs.put("yuanzhi", new ExCom("元智捷诚", "400-081-2345"));
		exComs.put("yuefeng", new ExCom("越丰快递", "852-23909969"));
		exComs.put("yunda", new ExCom("韵达快递", "400-821-6789"));
		exComs.put("yuntong", new ExCom("运通快递", "0769-81156999"));
		exComs.put("yuxing", new ExCom("宇鑫物流", "0371-66368798"));
		exComs.put("ywfex", new ExCom("源伟丰", "400-601-2228"));
		exComs.put("yxwl", new ExCom("宇鑫物流", "0371-66378798/99"));
		exComs.put("yzjc", new ExCom("元智捷诚快递", "400-081-2345"));
		exComs.put("zengyi", new ExCom("增益快递", "4008-456-789"));
		exComs.put("zhaijisong", new ExCom("宅急送", "400-6789-000"));
		exComs.put("zhongrui", new ExCom("中睿速递", "400-0375-888"));
		exComs.put("zhongsu", new ExCom("中速快递", "11183"));
		exComs.put("zhongtie", new ExCom("中铁快运", "400-000-5566"));
		exComs.put("zhongtong", new ExCom("中通快递", "021-39777777"));
		exComs.put("zhongwaiyung", new ExCom("中外运速递", "010-80418611"));
		exComs.put("zhongxinda", new ExCom("忠信达", "400-646-6665"));
		exComs.put("zhongyou", new ExCom("中邮物流", "11183"));
		exComs.put("zjs", new ExCom("宅急送快递", "400-6789-000"));
		exComs.put("zmkm", new ExCom("芝麻开门", "4001-056-056,88056056"));
		exComs.put("ztwy", new ExCom("中天万运快递", "0531-68850629"));
		exComs.put("zzjh", new ExCom("郑州建华快递", "0371-65995266"));
	}
	public static ExCom getExCom(String name) {
		return exComs.get(name);
	}
}
class ExCom {
	private String name;
	private String tel;
	public ExCom(String name, String tel) {
		this.name = name;
		this.tel = tel;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
}